# include "../include/Log.h"
# include <sstream>
# include <iomanip>
# include <ctime>
# include "../include/LogStream.h"
# include "../include/LogFile.h"

ZDSJ::Log::Log(): m_formater(new ZDSJ::Formater())
{
	
}

ZDSJ::Log::Log(const std::string& _formater) : m_formater(new ZDSJ::Formater(_formater))
{

}

ZDSJ::Log::Log(const ZDSJ::Formater& _formater) : m_formater(new ZDSJ::Formater(_formater))
{

}

ZDSJ::Log::Log(Level _level, const std::string& _formater) : m_formater(new ZDSJ::Formater(_formater)), m_level(_level)
{
}

ZDSJ::Log::Log(Level _level, const Formater& _formater) : m_formater(new ZDSJ::Formater(_formater)), m_level(_level)
{
}

ZDSJ::Log::~Log()
{
	delete this->m_formater;
}

void ZDSJ::Log::setTimeFormate(const std::string& _formate)
{
	this->m_time_format = _formate;
}

std::string ZDSJ::Log::getTimeFormate() const
{
	return this->m_time_format;
}

void ZDSJ::Log::setFileLevel(int _file_level)
{
	this->m_file_level = _file_level;
}

int ZDSJ::Log::getFileLevel() const
{
	return this->m_file_level;
}

void ZDSJ::Log::addStream(const LogStreamInterface* _stream)
{
	this->m_works.push_back(const_cast<LogStreamInterface*>(_stream));
	// std::find(this->m_works.begin(), this->m_works.end(), );
}

std::string ZDSJ::Log::getTime()
{
	std::stringstream oss;
	time_t now = std::time(nullptr);
	std::tm local_time;
	localtime_s(&local_time, &now);
	oss << std::put_time(&local_time, this->m_time_format.c_str());
	return std::move(oss.str());
}

std::string ZDSJ::Log::dealFileName(const std::string& _file_name)
{
	std::string file_name = _file_name;
	int index = file_name.size();
	for (int i = 0; i < this->m_file_level; ++i) {
		if (index == 0) {
			break;
		}
		index = file_name.find_last_of("\\", index - 1);
		if (index == std::string::npos) {
			break;
		}
	}
	file_name = file_name.substr(index + 1, file_name.size() - index);
	return std::move(file_name);
}
